package com.ruoyi.teaching.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.teaching.api.domain.FocusStudentDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.FocusMapper;
import com.ruoyi.teaching.domain.Focus;
import com.ruoyi.teaching.service.IFocusService;

/**
 * 重点关注Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-24
 */
@Service
public class FocusServiceImpl implements IFocusService 
{
    @Autowired
    private FocusMapper focusMapper;

    /**
     * 查询重点关注
     * 
     * @param id 重点关注主键
     * @return 重点关注
     */
    @Override
    public Focus selectFocusById(Long id)
    {
        return focusMapper.selectFocusById(id);
    }

    /**
     * 查询重点关注列表
     * 
     * @param focus 重点关注
     * @return 重点关注
     */
    @Override
    public List<Focus> selectFocusList(Focus focus)
    {
        return focusMapper.selectFocusList(focus);
    }

    /**
     * 新增重点关注
     * 
     * @param focus 重点关注
     * @return 结果
     */
    @Override
    public int insertFocus(Focus focus)
    {
        //1.校验该学生是否已被关注
        Focus focusVo = focusMapper.selectFocusByStudentId(focus.getStudentId());
        AssertUtil.isTrue(ObjectUtil.isNull(focusVo),"该学生已被关注，请勿反复关注！");
        focus.setCreateTime(DateUtils.getNowDate());
        return focusMapper.insertFocus(focus);
    }

    /**
     * 修改重点关注
     * 
     * @param focus 重点关注
     * @return 结果
     */
    @Override
    public int updateFocus(Focus focus)
    {
        return focusMapper.updateFocus(focus);
    }

    /**
     * 批量删除重点关注
     * 
     * @param ids 需要删除的重点关注主键
     * @return 结果
     */
    @Override
    public int deleteFocusByIds(Long[] ids)
    {
        return focusMapper.deleteFocusByIds(ids);
    }

    /**
     * 删除重点关注信息
     * 
     * @param id 重点关注主键
     * @return 结果
     */
    @Override
    public int deleteFocusById(Long id)
    {
        return focusMapper.deleteFocusById(id);
    }

    @Override
    public int addStudentFocus(FocusStudentDto focusStudentDto) {
        focusStudentDto.setTeacherId(SecurityContextHolder.getUserId());
        focusStudentDto.setTeacherName(SecurityContextHolder.getUserName());
        focusStudentDto.setClassName(focusStudentDto.getClasses());
        focusStudentDto.setStudentId(focusStudentDto.getId());
        focusStudentDto.setStudentState(Math.toIntExact(focusStudentDto.getStatus()));
        focusStudentDto.setStudentState(0);
        focusStudentDto.setDifficultyCoefficient(0);
        focusStudentDto.setCreateTime(new Date());
        focusStudentDto.setHappenTime(new Date());
        focusStudentDto.setId(null);
        return focusMapper.addStudentFocus(focusStudentDto);
    }
}
